//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.controller
{
	import air.update.ApplicationUpdaterUI;
	import air.update.events.UpdateEvent;
	
	import flash.events.ErrorEvent;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
	
	import reader.core.ReaderModelLocator;

	public class AutoUpdateCommand extends SimpleCommand implements ICommand
	{
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		private var appUpdater:ApplicationUpdaterUI;
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
				
		override public function execute(notification:INotification):void
		{
			appUpdater = new ApplicationUpdaterUI();
			appUpdater.updateURL = ReaderModelLocator.instance.updatePath;
			appUpdater.isCheckForUpdateVisible = false;
			appUpdater.addEventListener(UpdateEvent.INITIALIZED, onUpdate_handler);
    		appUpdater.addEventListener(ErrorEvent.ERROR, onError_handler);
    		appUpdater.initialize();
		}
		
		private function onUpdate_handler(e:UpdateEvent):void 
		{
		    appUpdater.checkNow();
		}
		
		private function onError_handler(e:ErrorEvent):void 
		{
		    Alert.show(e.toString());
		}


	}
}